import 'package:flutter/material.dart';

class ExpansionPanelDemo extends StatefulWidget {
  const ExpansionPanelDemo({Key? key, required this.children})
      : super(key: key);

  final List<Widget> children;

  @override
  State<ExpansionPanelDemo> createState() => _ExpansionPanelDemoState();
}

class _ExpansionPanelDemoState extends State<ExpansionPanelDemo> {
  bool _isExpanded = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(16.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          ExpansionPanelList(
            // 点击折叠按钮实现面板的伸缩
            expansionCallback: (int panelIndex, bool isExpanded) {
              setState(() {
                _isExpanded = !isExpanded;
              });
            },
            children: [
              ExpansionPanel(
                headerBuilder: (BuildContext context, bool isExpanded) {
                  return Container(
                    padding: EdgeInsets.all(16.0),
                    child: Text(
                      'Panel A',
                      style: Theme.of(context).textTheme.titleMedium,
                    ),
                  );
                },
                body: Container(
                  padding: EdgeInsets.all(16.0),
                  width: double.infinity,
                  child: Column(
                    children: widget.children,
                  ),
                ),
                isExpanded: _isExpanded, // 设置面板的状态，true展开，false折叠
              ),
            ],
          ),
        ],
      ),
    );
  }
}
